const redis = require('redis')	//引用redis模块

let redisConfig = require('../../../config/redis.json');


//新建客户端连接redis
const client = redis.createClient(redisConfig.redis.port, redisConfig.redis.host);
client.auth(redisConfig.redis.password);




client.on("connect", error => {
    if (!error) {
        console.log("connect to redis success")
    } else {
        console.error("connect to redis fial");
    }
});

class redisClient {

    //保存值
    static async setValue(key, value, timeout = 60) {
        return await client.set(key, value, 'EX', timeout);
    }

    //获取值
    static async getValue(key) {

        return new Promise((resolve, reject) => {
            client.get(key, (err, result) => {
                if (!!err) {
                    reject(null);
                } else {
                    resolve(result);
                }
            });
        });
    }


}


module.exports = redisClient;

